package com.example.okhttpexplain

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.okhttpexplain.ui.theme.OKHttpExplainTheme
import okhttp3.Authenticator
import okhttp3.Call
import okhttp3.Callback
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okhttp3.Route
import java.io.IOException
import java.net.InetAddress
import kotlin.concurrent.thread

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            OKHttpExplainTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Greeting("Android")
                }
            }
        }


        // 创建OkHttpClient实例
        val client = OkHttpClient()
        // 创建一个 HTTP 请求
        val url = "https://jsonplaceholder.typicode.com/posts/1"
        val request = Request.Builder()
            .url(url)
            .build()
        val response = client.newCall(request).enqueue(object: Callback{
            override fun onFailure(call: Call, e: IOException) {
            }

            override fun onResponse(call: Call, response: Response) {
                TODO("Not yet implemented")
            }
        })

        // 发送请求并获取响应
        client.newCall(request).execute().use { response ->
            if (!response.isSuccessful) {
                println("请求失败: ${response.code}")
                return
            }

            // 从响应中获取响应体数据
            val responseBody = response.body
            val responseBodyString = responseBody?.string()

            println("响应数据:")
            println(responseBodyString)
        }

        //根据域名获取ip地址
        thread {
            println("Resolved address: "+ InetAddress.getAllByName("www.baidu.com").toList()[0])
        }
    }

    fun testAuthen(){
        val client = OkHttpClient.Builder()
            .authenticator(object: Authenticator{
                override fun authenticate(route: Route?, response: Response): Request? {
                    //把token刷新
                    return response.request.newBuilder()
                        .header("Authorization", "xxx")
                        .build()
                }
            }).build()
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    OKHttpExplainTheme {
        Greeting("Android")
    }
}